package com.eling.elcms.travel.dao.hibernate;

import com.eling.elcms.core.dao.hibernate.GenericDaoHibernate;
import com.eling.elcms.travel.dao.IProductRuleDetailDao;
import com.eling.elcms.travel.model.ProductBatch;
import com.eling.elcms.travel.model.ProductRuleDetail;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
@Repository
public class ProductRuleDetailDaoHibernate extends GenericDaoHibernate<ProductRuleDetail, Long> implements IProductRuleDetailDao{

	public ProductRuleDetailDaoHibernate() {
		super(ProductRuleDetail.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductBatch> queryBatch(Long pkProductType) {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT                                                                    ");
		sql.append(" 	t2.pkProductType,t2.`name`,t3.pkProductBatch,t3.name                   ");
		sql.append(" FROM                                                                      ");
		sql.append(" 	`travel_productruledetail` t1                                          ");
		sql.append(" LEFT JOIN travel_producttype t2 ON t2.pkProductType = t1.pkProductType    ");
		sql.append(" LEFT JOIN travel_productbatch t3 ON t3.pkProductBatch=t1.pkProductBatch   ");
		sql.append(" WHERE                                                                     ");
		sql.append(" 	t2.seal=FALSE AND                                                      ");
		sql.append(" 	t2.pkProductType = :pkProductType;                                     ");
		List<Object[]> objList = getSession().createNativeQuery(sql.toString()).setParameter("pkProductType", pkProductType).list();
		List<ProductBatch> batchList = new ArrayList<>();
		for (int i = 0; i < objList.size(); i++) {
			ProductBatch productBatch = new ProductBatch();
			Object[] o = objList.get(i);
			productBatch.setPkProductBatch(o[2]==null?0:Long.parseLong(o[2].toString()));
			productBatch.setName(o[3]==null?"":o[2].toString());
			batchList.add(productBatch);
		}
		
		return batchList;
	}
}
